package com.zdy.viewpager;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItem;
import com.zdy.viewpager.PinnedSectionListActivity.Item;
import com.zdy.viewpager.PinnedSectionListView.PinnedSectionListAdapter;

public class DemoFragment extends Fragment implements OnItemClickListener {

  private MyAdapter myAdapter;
  private FragmentActivity activity;

  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_demo, container, false);
  }

  PinnedSectionListView lv;
  @Override
  public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    activity = getActivity();
    int position = FragmentPagerItem.getPosition(getArguments());
    TextView title = (TextView) view.findViewById(R.id.item_title);
    title.setText(String.valueOf(position));
    lv = (PinnedSectionListView) view.findViewById(R.id.lv);
    generateDataset('A', 'Z', false);
    myAdapter = new MyAdapter();
    lv.setOnItemClickListener(this);
    lv.setShadowVisible(false);
    lv.setAdapter(myAdapter);
  }

  class MyAdapter extends BaseAdapter implements PinnedSectionListAdapter {
    public MyAdapter() {
    }

    private final int[] COLORS = new int[] { R.color.green_light, R.color.orange_light, R.color.blue_light,
        R.color.red_light };

    @Override
    public int getCount() {
      return list.size();
    }

    @Override
    public Object getItem(int position) {
      return list.get(position);
    }

    @Override
    public long getItemId(int position) {
      return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      TextView view = new TextView(activity);
      view.setTextColor(Color.BLACK);
      view.setTextSize(22);
      view.setTag("" + position);
      Item item = list.get(position);
      if (item.type == Item.SECTION) {
        // view.setOnClickListener(PinnedSectionListActivity.this);
        view.setBackgroundColor(parent.getResources().getColor(COLORS[item.sectionPosition % COLORS.length]));
        view.setText(list.get(position).text);
      }else {
        view.setText(list.get(position).text);
      }
      return view;
    }

    @Override
    public int getViewTypeCount() {
      return 2;
    }

    @Override
    public int getItemViewType(int position) {
      return list.get(position).type;
    }

    @Override
    public boolean isItemViewTypePinned(int viewType) {
      return viewType == Item.SECTION;
    }

  }

  class Item {

    public static final int ITEM = 0;
    public static final int SECTION = 1;

    public final int type;
    public final String text;

    public int sectionPosition;
    public int listPosition;

    public Item(int type, String text) {
      this.type = type;
      this.text = text;
    }

    @Override
    public String toString() {
      return text;
    }

  }

  List<Item> list = new ArrayList<Item>();

  public void generateDataset(char from, char to, boolean clear) {

    final int sectionsNumber = to - from + 1;
    int sectionPosition = 0, listPosition = 0;
    for (char i = 0; i < sectionsNumber; i++) {
      Item section = new Item(Item.SECTION, String.valueOf((char) ('A' + i)));
      section.sectionPosition = sectionPosition;
      section.listPosition = listPosition++;
      list.add(section);

      final int itemsNumber = (int) Math.abs((Math.cos(2f * Math.PI / 3f * sectionsNumber / (i + 1f)) * 25f));
      for (int j = 0; j < itemsNumber; j++) {
        Item item = new Item(Item.ITEM, section.text.toUpperCase(Locale.ENGLISH) + " - " + j);
        item.sectionPosition = sectionPosition;
        item.listPosition = listPosition++;
        list.add(item);
      }

      sectionPosition++;
    }
  }

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Item item =  (Item) lv.getItemAtPosition(position);
    if (item != null) {
        Toast.makeText(getActivity(), "Item " + position + ": " + item.text, Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(getActivity(), "Item " + position, Toast.LENGTH_SHORT).show();
    }
  }
}
